package com.feicheng.common.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.sms.SmsUtils;
import com.feicheng.persistence.entity.Cron;
import com.feicheng.persistence.entity.Vip;
import com.feicheng.persistence.mapper.CronMapper;
import com.feicheng.persistence.model.VipMain;
import com.feicheng.persistence.service.IVipService;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import javax.management.Query;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

@Component
@EnableScheduling
@Slf4j
public class TelMessageTask implements SchedulingConfigurer {

    @Autowired
    CronMapper cronMapper;

    @Autowired
    IVipService vipService;

    @Autowired
    SmsUtils smsUtils;

    private Cron cron;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
                () -> {
                    log.info("定时任务："+ LocalDateTime.now());
                    if (null != cron) {
                        String bjs = cron.getBjs();
                        List<Integer> bjList = JSONObject.parseArray(bjs, Integer.class);
                        bjList.sort((Integer o1, Integer o2) -> {
                            return o1 - o2;
                        });

                        List<QueryWrapper<VipMain>> qws = getQws(bjList);

                        for (Iterator<QueryWrapper<VipMain>> it = qws.iterator(); it.hasNext();) {
                            QueryWrapper<VipMain> qw = it.next();
                            checkRestDays(qw);

                        }
                    }
                },
                triggerContext -> {
                    cron = cronMapper.selectById(1);
                    return new CronTrigger(cron.getCron()).nextExecutionTime(triggerContext);
                });
    }

    private List<QueryWrapper<VipMain>> getQws(List<Integer> list) {
        List<QueryWrapper<VipMain>> result = new ArrayList<QueryWrapper<VipMain>>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime st = LocalDateTime.now();
        while (list.size() > 0) {

            Integer r = list.remove(0);
            LocalDateTime et = now.plusDays(r);
            QueryWrapper<VipMain> qw = new QueryWrapper<>();
            qw.eq("statu", 1);
            qw.lt("et", et.plusDays(0));
            qw.ge("et", st.plusDays(0));
            result.add(qw);

//            log.info("【"+st.plusDays(0).toString()+"】-【"+et.plusDays(0).toString()+"】");
            st = et.plusDays(0);
        }

        return result;
    }


    private void checkRestDays(QueryWrapper<VipMain> vipQueryWrapper) {

        JSONArray phoneNumbers = new JSONArray();
        JSONArray templateParam = new JSONArray();
        List<VipMain> vipMainList = vipService.getVips(vipQueryWrapper);
        for (Iterator<VipMain> it = vipMainList.iterator(); it.hasNext();) {
            VipMain vip = it.next();
            log.info(vip.getPhone()+":"+vip.getEt().getYear() + "/" + vip.getEt().getMonth().getValue() + "/" + vip.getEt().getDayOfMonth());

            phoneNumbers.add(vip.getPhone());
            templateParam.add(vip.getEt().getYear());
            templateParam.add(vip.getEt().getMonth().getValue());
            templateParam.add(vip.getEt().getDayOfMonth());

            try {
                smsUtils.sendMessage(phoneNumbers, templateParam);
            } catch (TencentCloudSDKException e) {
                log.error("发送短信出现问题：", e);
            }
        }

    }
}
